<?php

class publicationsActions extends sfActions {

    public function executeIndex(sfWebRequest $request){
        $c = new Criteria();
        $c->add(PublicationsPeer::ENABLED, 1);
        $c->addAscendingOrderByColumn(PublicationsPeer::PUBLICATIONS_TYPE_ID);
        $c->addAscendingOrderByColumn(PublicationsPeer::TITLE);

        $pager = new sfPropelPager('Publications', sfConfig::get('app_max_per_page'));
        $pager->setCriteria($c);
        $pager->setPage($request->getParameter('page', 1));
        $pager->init();
        $this->pager = $pager;
    }

    public function executeNew(sfWebRequest $request){
        $this->publications_types = PublicationsTypePeer::doSelect(new Criteria());
    }

    public function executeNew_save(sfWebRequest $request){
        $title = $request->getParameter('title');
        $author = $request->getParameter('author');
        $release_date = $request->getParameter('release_date');
        $type = $request->getParameter('type');
        $image = $_FILES["image"]["tmp_name"];

        $day = substr($release_date,0,2);
        $month = substr($release_date,3,2);
        $year = substr($release_date,6,4);
        $f_release_date = $year."-".$month."-".$day;

        $publication = new Publications();
        $publication->setTitle($title);
        $publication->setAuthor($author);
        $publication->setReleaseDate($f_release_date);
        $publication->setPublicationsTypeId($type);
        $publication->setEnabled(1);
        $publication->setCreatedAt(date("Y-m-d H:i:s"));
        $publication->save();

        if($image!=''){
            $DS = DIRECTORY_SEPARATOR;
            move_uploaded_file($image, sfConfig::get('sf_upload_dir').$DS."publications".$DS.$publication->getId().".jpg");           
            }

        $this->getUser()->setFlash('msg','Publicación creada satisfactoriamente');
        $this->redirect('@publications');
    }

    public function executeEdit(sfWebRequest $request){
        $id = $request->getParameter('id');
        $this->publication = PublicationsPeer::retrieveByPK($id);
        $this->publications_types = PublicationsTypePeer::doSelect(new Criteria());
    }

    public function executeEdit_save(sfWebRequest $request){
        $hdId = $request->getParameter('hdId');
        $title = $request->getParameter('title');
        $author = $request->getParameter('author');
        $release_date = $request->getParameter('release_date');
        $type = $request->getParameter('type');
        $image = $_FILES["image"]["tmp_name"];

        $day = substr($release_date,0,2);
        $month = substr($release_date,3,2);
        $year = substr($release_date,6,4);
        $f_release_date = $year."-".$month."-".$day;

        $publication = PublicationsPeer::retrieveByPK($hdId);
        $publication->setTitle($title);
        $publication->setAuthor($author);
        $publication->setReleaseDate($f_release_date);
        $publication->setPublicationsTypeId($type);
        $publication->save();

        if($image!=''){
            $DS = DIRECTORY_SEPARATOR;
            move_uploaded_file($image, sfConfig::get('sf_upload_dir').$DS."publications".$DS.$publication->getId().".jpg");
        }

        $this->getUser()->setFlash('msg','Publicación modificada satisfactoriamente');
        $this->redirect('@publications');
    }

    public function executeDelete(sfWebRequest $request){
        $id = $request->getParameter('id');
        $publication = PublicationsPeer::retrieveByPK($id);
        $publication->setEnabled(0);
        $publication->save();

        $this->getUser()->setFlash('msg','Publicación eliminada satisfactoriamente');
        $this->redirect('@publications');
    }

}